/**
 * nwfbiz
 * 流程关联业务配置页面
 * 对流程关联业务类型以及对该流程的节点配置业务页面
 * reform-新增业务类型
 * editform-修改业务类型
 * configfrom-新增和修改节点配置
 * Created by zhouqh on 2020/1/16.
 */
define([
], function (require, exports) {
  exports.ready = function (hashCode, data, cite) {
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        var _this = this;
        return {
          formdata: {},
          urls: {
            index: backend.workflowService + '/api/biz/',
            update: backend.workflowService + '/api/biz/update/',
            select: backend.workflowService + '/api/biz/select',
            batchdelete: backend.workflowService + '/api/biz/delete/',
            bizNode: backend.workflowService + '/api/biz/node/',
            bizNodeUpdate: backend.workflowService + '/api/biz/node/update',
            bizNodeDelete: backend.workflowService + '/api/biz/node/delete',
            getFlowUrl: backend.workflowService + '/api/nwfflow/',
            getNodeUrl: backend.workflowService + '/api/biz/allnode/'
          },
          tableColumns: [
            { label: '业务类型', prop: 'bizType', resizable: true},
            { label: '业务类型描述', prop: 'ext', resizable: true },
            { label: '流程编号', prop: 'flowId', resizable: true},
            { label: '流程名称', prop: 'flowName', resizable: true},
            { label: '业务页面', prop: 'pageUrl', resizable: true},
            {
              label: '操作',
              width: 120,
              fixed: 'right',
              template: function () {
                return '<template scope="scope">\
                <el-button size="small" type="text" @click="$emit(\'custom-row-op\',scope,\'openBizNode\')">配置</el-button>\
                </template>';
              }
            }
          ],
          bizNodeTableColumns: [
            { label: '节点编号', prop: 'nodeId', resizable: true},
            { label: '节点名称', prop: 'nodeName', resizable: true},
            { label: '业务页面', prop: 'pageUrl', resizable: true},
            { label: '流程编号', prop: 'flowId', resizable: true},
            { label: '业务类型', prop: 'bizType', resizable: true},
            { label: '业务类型描述', prop: 'ext', resizable: true},
            {
              label: '操作',
              width: 120,
              fixed: 'right',
              template: function () {
                return '<template scope="scope">\
                <el-button size="small" type="text" @click="$emit(\'custom-row-op\',scope,\'config\')">配置</el-button>\
                </template>';
              }
            },
            { label: '当前机构', prop: 'orgId', resizable: true, hidden: true, align: 'center' }
          ],
          newFields: [
            {
              columnCount: 2,
              fields: [
                { label: '业务类型', field: 'bizType', type: 'input', disabled: false, rules: [{ required: true, message: '请输入业务类型标识符', swagger: 'blur' }] },
                { label: '描述', field: 'ext', type: 'input', disabled: false, rules: [{ required: true, message: '请输入业务类型标题或者描述', swagger: 'blur' }] },
                {
                  label: '关联流程',
                  placeholder: '请点击尾部查询按钮',
                  field: 'flowId',
                  icon: 'search',
                  type: 'num',
                  readonly: true,
                  click: function () {
                    _this.title = 'FLOW';
                    _this.flowdialogVisible = true;
                    _this.$nextTick(function () {
                      _this.$refs.search.$children[0].resetFields();
                      _this.$refs.flowList.remoteData({});
                    });
                  },
                  rules: [{ required: true, message: '请选择要关联的流程' }]
                },
                { label: '流程名称', field: 'flowName', type: 'input', disabled: true, rules: [{ required: true, message: '必填项', swagger: 'blur' }] },
                { label: '当前机构', field: 'orgId', value: yufp.session.org.code, type: 'input', hidden: true }
              ]
            }, {
              columnCount: 1,
              fields: [
                { label: '业务页面', field: 'pageUrl', type: 'input', rules: [{ required: true, message: '请配置业务页面ID', swagger: 'blur' }] }
              ]
            }
          ],
          editFields: [
            {
              columnCount: 2,
              fields: [
                { label: '业务类型', field: 'bizType', type: 'input', disabled: true, rules: [{ required: true, message: '请输入业务类型标识符', swagger: 'blur' }] },
                { label: '描述', field: 'ext', type: 'input', disabled: true, rules: [{ required: true, message: '请输入业务类型标题或者描述', swagger: 'blur' }] },
                { label: '当前机构', field: 'orgId', value: yufp.session.org.code, type: 'input', hidden: true }
              ]
            }, {
              columnCount: 1,
              fields: [
                { label: '业务页面', field: 'pageUrl', type: 'input', rules: [{ required: true, message: '请配置业务页面ID', swagger: 'blur' }] }
              ]
            }
          ],
          configFields: [
            {
              columnCount: 2,
              fields: [
                { label: '业务类型', field: 'bizType', type: 'input', disabled: true, rules: [{ required: true, message: '业务类型标识符', swagger: 'blur' }] },
                { label: '关联流程', field: 'flowId', type: 'input', disabled: true, rules: [{ required: true, message: '必填项' }] },
                {
                  label: '关联节点',
                  placeholder: '请点击尾部查询按钮',
                  field: 'nodeId',
                  icon: 'search',
                  type: 'num',
                  disabled: true,
                  click: function () {
                    _this.title = 'NODE';
                    if (_this.configType == 'CONFIG') {
                      return;
                    }
                    _this.nodedialogVisible = true;
                    _this.$nextTick(function () {
                      _this.$refs.nodeTable.remoteData();
                    });
                  },
                  rules: [{ required: true, message: '必填项', swagger: 'blur' }]
                },
                { label: '节点名称', field: 'nodeName', type: 'input', disabled: true, rules: [{ required: true, message: '必填项' }] }
              ]
            },
            {
              columnCount: 1,
              fields: [
                { label: '业务页面', field: 'pageUrl', type: 'input', disabled: false, rules: [{ required: true, message: '请配置业务页面ID', swagger: 'blur' }] }
              ]
            }
          ],
          queryflowFileds: [
            { placeholder: '流程编号', field: 'flowId', type: 'input' },
            { placeholder: '流程名称', field: 'flowName', type: 'input' }
          ],
          queryflow: {
            flowId: '',
            flowName: ''
          },
          flowTableColumns: [
            { label: '流程编号', prop: 'flowId', resizable: true, align: 'center' },
            { label: '流程名称', prop: 'flowName', resizable: true, align: 'center' }
          ],
          nodeTableColumns: [
            { label: '节点编号', prop: 'nodeId', resizable: true, align: 'center' },
            { label: '节点名称', prop: 'nodeName', resizable: true, align: 'center' }
          ],
          flowParams: {
            condition: JSON.stringify({ flowState: 'R' })
          },
          baseParams: {
            condition: {}
          },
          nodeParams: {},
          bizNodeParams: { condition: {} },
          flowButtons: [
            {
              label: '搜索',
              op: 'submit',
              type: 'primary',
              show: true,
              click: function (model, valid) {
                if (valid) {
                  var params = {};
                  params.flowId = model.flowId;
                  if (model.flowName != null) {
                    params.flowName = '%' + model.flowName + '%';
                  }
                  var param = {
                    condition: JSON.stringify(params)
                  };
                  _this.$refs.flowList.remoteData(param);
                }
              }
            },
            { label: '重置', op: 'reset', show: this.resetButton }
          ],
          height: yufp.frame.size().height - 103,
          newdialogVisible: false,
          editdialogVisible: false,
          configdialogVisible: false,
          flowdialogVisible: false,
          viewType: 'ADD',
          title: 'EDIT',
          configType: 'NEWCONFIG',
          viewTitle: {// 弹框类型以及对应标题
            ADD: '新增',
            EDIT: '修改',
            FLOW: '流程选取',
            NODE: '节点选取',
            BIZNODE: '节点外挂业务页面详情',
            NEWCONFIG: '新增节点外挂业务页面',
            CONFIG: '修改节点外挂业务页面'
          },
          nodedialogVisible: false,
          bizNodedialogVisible: false,
          bizModel: {}
        };
      },
      methods: {
        customRowOp: function (scope, op) { // 表格操作按钮事件
          var _this = this;
          // 当前行号：scope.$index
          // 当前行数据：scope.row
          // 当前列对象：scope.column
          // this.$alert('你现在正在操作：' + op + '当前行ID值为：' + scope.row.flowId, '提示');
          if (op == 'config') { // 业务关联节点配置
            _this.configType = 'CONFIG';
            _this.configdialogVisible = true;
            _this.$nextTick(function () {
              _this.$refs.configform.resetFields();
              yufp.extend(_this.$refs.configform.formModel, scope.row);
            });
          } else if (op == 'openBizNode') { // 打开业务关联节点配置详情页
            _this.viewType = 'BIZNODE';
            _this.bizModel = scope.row;
            _this.bizNodeParams = { condition: { bizType: scope.row.bizType } };
            _this.nodeParams = { flowId: scope.row.flowId };
            _this.bizNodedialogVisible = true;
          }
        },
        addFn: function () {
          var _this = this;
          _this.viewType = 'ADD';
          _this.newdialogVisible = true;
          _this.$nextTick(function () {
            _this.$refs.reform.resetFields();
          });
        },
        sureAdd: function () { // 新增业务类型并关联流程
          var _this = this;
          var model = _this.$refs.reform.formModel;
          _this.$refs.reform.validate(function (valid) {
            if (!valid) {
              _this.$message({ duration: 4000, message: '请检查输入项是否合法!', type: 'warning' });
              return;
            }
            model.orgId = yufp.session.org.code;
            yufp.service.request({// 业务类型唯一性验证
              url: _this.urls.select,
              data: { bizType: model.bizType },
              method: 'GET',
              callback: function (code, message, response) {
                if (response.data != null && response.data.length != 0) {
                  _this.$message({ duration: 4000, message: '业务类型唯一性校验不通过！', type: 'warning' });
                } else {
                  yufp.service.request({
                    method: 'POST',
                    url: _this.urls.index,
                    data: model,
                    callback: function (code, message, response) {
                      if (response.code == 0) {
                        _this.$refs.reftable.remoteData();
                        _this.$message({ duration: 6000, message: '新增成功!', type: 'success' });
                        _this.newdialogVisible = false;
                      } else {
                        _this.$message({ duration: 4000, message: '新增异常:' + response.message.substring(0, 100), type: 'error' });
                      }
                    }
                  });
                }
              }
            });
          });
        },
        modifyFn: function () { // 修改业务类型关联流程配置
          this.viewType = 'EDIT';
          var selections = this.$refs.reftable.selections;
          if (selections.length < 1) {
            this.$message({ duration: 4000, message: '请选择一条记录', type: 'warning' });
            return;
          }
          var obj = selections[0];
          this.editdialogVisible = true;
          this.$nextTick(function () {
            yufp.extend(this.$refs.editform.formModel, obj);
          });
        },
        sureModify: function () {
          var _this = this;
          var model = _this.$refs.editform.formModel;
          _this.$refs.editform.validate(function (valid) {
            if (!valid) {
              _this.$message({ duration: 4000, message: '请检查输入项是否合法!', type: 'warning' });
              return;
            }
            model.orgId = yufp.session.org.code;
            yufp.service.request({
              method: 'POST',
              url: _this.urls.update,
              data: model,
              callback: function (code, message, response) {
                if (response.code == 0) {
                  _this.$refs.reftable.remoteData();
                  _this.$refs.editform.resetFields();
                  _this.$message({ duration: 4000, message: '修改业务配置成功!', type: 'success' });
                  _this.editdialogVisible = false;
                } else {
                  _this.$message({ duration: 4000, message: '修改异常:' + response.message, type: 'error' });
                }
              }
            });
          });
        },
        deleteFn: function () { // 删除业务类型相关配置
          var _this = this;
          if (_this.$refs.reftable.selections.length < 1) {
            _this.$message({ duration: 4000, message: '请选择至少一条记录', type: 'warning' });
            return;
          }
          var row = _this.$refs.reftable.selections[0];
          _this.$confirm('确定要删除吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            yufp.service.request({
              method: 'POST',
              url: _this.urls.batchdelete + row.bizType + '/' + row.flowId,
              callback: function (code, message, response) {
                if (response.code == 0) {
                  _this.$refs.reftable.remoteData();
                  _this.$message({ duration: 4000, message: '成功删除' + response.data + '条记录', type: 'success' });
                } else {
                  _this.$message({ duration: 4000, message: '删除异常:' + response.message.substring(0, 100), type: 'error' });
                }
              }
            });
          }).catch(function (e) { });
        },
        newBizNodeConfig: function () {
          var _this = this;
          _this.configType = 'NEWCONFIG';
          _this.configdialogVisible = true;
          _this.$nextTick(function () {
            _this.$refs.configform.resetFields();
            yufp.extend(_this.$refs.configform.formModel, _this.bizModel);
          });
        },
        sureConfig: function () {
          var _this = this;
          var param = _this.$refs.configform.formModel;
          var url = _this.urls.bizNodeUpdate;
          var Message = '业务类型关联节点配置修改成功!';
          if (_this.configType == 'NEWCONFIG') {
            url = _this.urls.bizNode;
            Message = '业务类型关联节点配置新增成功!';
          }
          _this.$refs.configform.validate(function (valid) {
            if (valid) {
              yufp.service.request({
                method: 'POST',
                url: url,
                data: param,
                callback: function (code, message, response) {
                  if (response.code == 0) {
                    _this.$refs.bizNodeTable.remoteData();
                    _this.$message({ duration: 4000, message: Message, type: 'success' });
                    _this.configdialogVisible = false;
                  } else {
                    _this.$message({ duration: 4000, message: '节点配置异常:' + response.message.substring(0, 100), type: 'error' });
                  }
                }
              });
            } else {
              _this.$message({ duration: 4000, message: '请检查输入项是否合法!', type: 'warning' });
            }
          });
        },
        deleteBizNodeConfig: function () {
          if (this.$refs.bizNodeTable.selections.length !== 1) {
            this.$message({ duration: 4000, message: '请选择一条数据!', type: 'warning' });
            return false;
          }
          var _this = this;
          var row = _this.$refs.bizNodeTable.selections[0];
          _this.$confirm('确定要删除吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            yufp.service.request({
              method: 'POST',
              url: _this.urls.bizNodeDelete + '/' + row.bizType + '/' + row.flowId + '/' + row.nodeId,
              callback: function (code, message, response) {
                if (response.code == 0) {
                  _this.$refs.bizNodeTable.remoteData();
                  _this.$message({ duration: 4000, message: '成功删除' + response.data + '条记录', type: 'success' });
                } else {
                  _this.$message({ duration: 4000, message: '删除异常:' + response.message.substring(0, 100), type: 'error' });
                }
              }
            });
          }).catch(function (e) { });
        },
        refreshBizNodeConfig: function () {
          if (this.bizModel != null) {
            var param = { condition: { bizType: this.bizModel.bizType } };
            this.$nextTick(function () {
              this.$refs.bizNodeTable.remoteData(param);
            });
          }
        },
        returnflow: function () { // 选取流程标识返回
          if (this.$refs.flowList.selections.length !== 1) {
            this.$message({ duration: 4000, message: '请选择一条数据!', type: 'warning' });
            return false;
          }
          var row = this.$refs.flowList.selections[0];
          this.$nextTick(function () {
            this.$refs.reform.formModel.flowId = row.flowId;
            this.$refs.reform.formModel.flowName = row.flowName;
            this.flowdialogVisible = false;
          });
        },
        returnNode: function () {
          if (this.$refs.nodeTable.selections.length !== 1) {
            this.$message({ duration: 4000, message: '请选择一条数据!', type: 'warning' });
            return false;
          }
          var row = this.$refs.nodeTable.selections[0];
          this.$nextTick(function () {
            this.$refs.configform.formModel.nodeId = row.nodeId;
            this.$refs.configform.formModel.nodeName = row.nodeName;
            this.nodedialogVisible = false;
          });
        }
      }
    });
  };
});
